package com.ts.opengl.simpleDraw

import android.content.Context
import android.opengl.GLSurfaceView
import android.opengl.Matrix
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import com.ts.opengl.IInterface.TransformRender
import kotlin.math.max
import kotlin.math.min


class DemoOpenglSurface: GLSurfaceView, ScaleGestureDetector.OnScaleGestureListener {
    private var mScaleGestureDetector: ScaleGestureDetector? = null
//    var Projection=FloatArray(4*4){0.0f}
//      var mTransformRender: TransformRender ?= null

//     var mtransformRender:transformRender

    var mtransformRender=TexttureRender(context)
    constructor(context: Context,att:AttributeSet):super(context,att){
        setEGLContextClientVersion(2)
//        mtransformRender=TexttureRender(context)
//        setEGLConfigChooser()
        setRenderer(mtransformRender)

        renderMode= RENDERMODE_WHEN_DIRTY
        mScaleGestureDetector = ScaleGestureDetector(context, this)

//        Matrix.perspectiveM(Projection,0,45.0f,
//            0.501393f,0.1f,100.0f)
//        Matrix.orthoM()
//        Matrix.setLookAtM()
//        Matrix.perspectiveM()
//        logMatrix(Projection,"Projection1")
//        var dd=0;
    }

    fun logMatrix( result:FloatArray,TAG1:String="矩阵"){
        var conte=""
        result.forEachIndexed { index, fl ->

            conte+=result.get(index).toString()+" "


        }
        Log.e(TAG,TAG1+"矩阵:"+conte)
    }
    private val TOUCH_SCALE_FACTOR = 180.0f / 320

    private var mPreScale = 1.0f
    private var mCurScale = 1.0f


    private var mPreviousY = 0f
    private var mPreviousX = 0f

    private var mXAngle = 0
    private var mYAngle = 0

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.pointerCount==1){ //获取手指的个数
            val currentTimeMillis = System.currentTimeMillis()
            if (currentTimeMillis - mLastMultiTouchTime > 200){
                val y=event.y
                val x=event.x
                Log.e(TAG, "--$x--juejin--$y"+event.action)
                if (event.action==MotionEvent.ACTION_MOVE){
                    val dy=y-mPreviousY;
                    val dx=x-mPreviousX;
                    mYAngle += (dy * TOUCH_SCALE_FACTOR).toInt()
                    mXAngle += (dx* TOUCH_SCALE_FACTOR).toInt()
                }

                mPreviousY = y
                mPreviousX = x
                Log.e(TAG, "$mYAngle mYAngle $mXAngle")

                mtransformRender?.UpdateTransformMatrix(mXAngle*1.0f, mYAngle*1.0f,mCurScale,mCurScale)
                requestRender()



            }

        }else {
            event?.let { mScaleGestureDetector?.onTouchEvent(it) }
        }
        return true
    }

    private  val TAG = "DemoOpenglSurface"

    override fun onScale(detector: ScaleGestureDetector): Boolean {
        Log.d(TAG,"X:" + detector.focusX);
        Log.d(TAG,"Y:" + detector.focusY);
        Log.d(TAG,"scale:" + detector.getScaleFactor()+
                "---"+TOUCH_SCALE_FACTOR)
        val preSpan=mScaleGestureDetector?.previousSpan;
        val curSpan=mScaleGestureDetector?.currentSpan;
        Log.e(TAG,preSpan?.toString()+"---"+curSpan)
        mCurScale = if (curSpan!! < preSpan!!){
            mPreScale - (preSpan - curSpan) / 200
        }else{
            mPreScale + (curSpan - preSpan) / 200
        }
        mCurScale = max(0.05, min(mCurScale.toDouble(), 80.0)).toFloat()
        Log.e(TAG,mCurScale.toString()+"mCurScale")
        mtransformRender?.UpdateTransformMatrix(mXAngle.toFloat(), mYAngle.toFloat(),mCurScale,mCurScale)
        requestRender()
        return true;
    }

    override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
        Log.e(TAG,"onScale")
        return true;
    }
    private var mLastMultiTouchTime: Long = 0
    override fun onScaleEnd(detector: ScaleGestureDetector) {
        Log.e(TAG,"onScale")
        mPreScale = mCurScale
        mLastMultiTouchTime = System.currentTimeMillis()
    }

    private val mRatioWidth = 0
    private val mRatioHeight = 0
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)

        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height)
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth)
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height)
            }
        }
    }
}